import 'package:flutter/material.dart';

typedef OnSliverHeaderChange = void Function(
  bool isOffset,
  double shrinkOffset,
);

class KKCommonSliverHeaderDelegate extends SliverPersistentHeaderDelegate {
  double minHeight;
  double? maxHeight;
  Widget child;
  OnSliverHeaderChange? onSliverChange;

  KKCommonSliverHeaderDelegate({
    required this.minHeight,
    this.maxHeight,
    required this.child,
    this.onSliverChange,
  });

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    onSliverChange?.call(shrinkOffset > 0, shrinkOffset);
    return child;
  }

  @override
  double get maxExtent => maxHeight ?? minHeight;

  @override
  double get minExtent => minHeight;

  @override
  bool shouldRebuild(covariant KKCommonSliverHeaderDelegate oldDelegate) {
    return oldDelegate.minHeight != minHeight ||
        oldDelegate.maxHeight != maxHeight ||
        oldDelegate.child != child;
  }
}
